﻿using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Text;

namespace Outcoder.Calcium.VSIntegration.Installation
{
	class LegacyInstaller
	{
		public static void UninstallLegacy(string vsVersion, Action<string> logAction)
		{
			string filePartName = Tools.GetFilePartName(vsVersion);
			/* HACK: The VS Setup project removes files after this method is called, 
			 * therefore we must remove the file before we call devenv /InstallVsTemplates. */
			try
			{
				/* C# Template Removal */
				string csWinTemplateDir = Tools.GetVSProjectTemplatesDir(vsVersion, "CSharp", "Windows");
				string csWinItemTemplatesDir = Tools.GetVSItemTemplatesDir(vsVersion, "CSharp", "WPF");
				string fileName = csWinTemplateDir + filePartName + "CalciumCSWinLauncher.zip";
				bool existed = Tools.DeleteFileIfExists(fileName);
				fileName = csWinTemplateDir + filePartName + "CalciumCSWinModule.zip";
				existed |= Tools.DeleteFileIfExists(fileName);
				fileName = csWinItemTemplatesDir + filePartName + "CalciumCSWinView.zip";
				existed |= Tools.DeleteFileIfExists(fileName);

				string pathToCSWebTemplate2008 = Tools.GetVSProjectTemplatesDir(vsVersion, "CSharp", "Web");
				fileName = pathToCSWebTemplate2008 + filePartName + "CalciumCSWeb.zip";
				existed |= Tools.DeleteFileIfExists(fileName);

				/* VB.NET Template Removal */
				string vbWinTemplateDir = Tools.GetVSProjectTemplatesDir(vsVersion, "VisualBasic", "Windows");
				fileName = vbWinTemplateDir + filePartName + "CalciumVBWinLauncher.zip";
				existed |= Tools.DeleteFileIfExists(fileName);
				fileName = vbWinTemplateDir + filePartName + "CalciumVBWinModule.zip";
				existed |= Tools.DeleteFileIfExists(fileName);

				string pathToVBWebTemplate2008 = Tools.GetVSProjectTemplatesDir(vsVersion, "VisualBasic", "Web");
				fileName = pathToVBWebTemplate2008 + filePartName + "CalciumVBWeb.zip";
				existed |= Tools.DeleteFileIfExists(fileName);

				string pathToVBWpfTemplate2008 = Tools.GetVSProjectTemplatesDir(vsVersion, "VisualBasic", "WPF");
				fileName = pathToVBWpfTemplate2008 + filePartName + "CalciumVBWinView.zip";
				existed |= Tools.DeleteFileIfExists(fileName);

				if (existed)
				{
					string vsInstallDir = Tools.GetInstallDir(vsVersion, logAction);
					Tools.RunInstallTemplatesUsingDir(vsInstallDir, logAction);
				}
			}
			catch (Exception ex)
			{
				logAction("Unable to call remove VS2008 VS Templates. " + ex.ToString());
			}
		}

		public static void CommitLegacy(string vsVersion, Action<string> logAction)
		{
			//MessageBox.Show("Committing custom action.");
			try
			{
				string installDir = Tools.GetInstallDir(vsVersion, logAction);
				Tools.RunInstallTemplatesUsingDir(installDir, logAction);
			}
			catch (Exception ex)
			{
				logAction(string.Format(
					"Unable to call run install template for VS Version {0} Exception: {1}", 
					vsVersion, ex.ToString()));
			}
		}
	}
}
